import { Request, Response } from 'express';
import mockjs from 'mockjs';

const delay = (ms:number)=> new Promise(resolve=>{
  setTimeout(resolve,ms)
})

export default {

  // 1.1 查询
  'GET /api/customer/list': async (req: Request, res: Response) => {
    const {
      query: { page },
    } = req;

    await delay(300)
    res.send(
      mockjs.mock({
        data: {
          'customers|100': [
            {
              id: '@id',
              name: '@name',
              'age|1-100': 1,
              'height|130-200.1': 130,
              'weight|40-100.1': 40,
              'city': '@city',
              'work|1': true,
            },
          ],
          total: 100,
          size: 10,
          page: Number(page) || 1,
        },
        code: '200',
        message: 'ok',
        request_id: '56e83d08ea1e4c209af9b857c2a9abf8',
      }),
    );
  },

  // 1.2 新增
  'POST /api/customer/add': async (req: Request, res: Response) => {
    // console.log(req);
    await delay(300)
    res.send(
      mockjs.mock({
        code: '200',
        message: '添加成功',
        request_id: '56e83d08ea1e4c209af9b857c2a9abf8',
      }),
    );

  },

  // 1.3 编辑
  'PUT /api/customer/update': async (req: Request, res: Response) => {

    await delay(300)
    res.send(
      mockjs.mock({
        code: '200',
        message: '编辑成功1',
        request_id: '56e83d08ea1e4c209af9b857c2a9abf8',
      }),
    );

  },

  // 1.4 删除
  'DELETE /api/customer/delete': async (req: Request, res: Response) => {
    // console.log(req);
    // await delay(300)

    res.send(
      mockjs.mock({
        code: '200',
        message: '删除成功',
        request_id: '56e83d08ea1e4c209af9b857c2a9abf8',
      }),
    );
  },

};
